<template>
  <div>
    <div class="warn-top">
      <h4 style="text-align: left; color: #126bff">班次管理</h4>
      <!--         :rules="rules" -->
      <el-form
        ref="elForm"
        :model="formData"
        size="medium"
        label-width="80px"
        style="display: flex"
      >
        <el-form-item label="班次名称:" prop="name" style="width: 450px">
          <el-input
            v-model="formData.name"
            placeholder="请输入班次名称"
            clearable
            :style="{ width: '80%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item size="small " style="position: relative; right: 20px">
          <el-button @click="resetForm()" id="reset">重置</el-button>
          <el-button
            type="primary"
            @click="submitForm(formData)"
            id="submitForm"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="warn-content" style="width: 100%">
      <div>
        <el-button
          icon="el-icon-plus"
          @click="addTeam"
          style="
            height: 36px;
            background-color: #126bff;
            border-radius: 6px;
            opacity: 1;
            color: #ffffff;
            font-size: 14px;
            font-family: Microsoft YaHei-Regular, Microsoft YaHei;
            font-weight: 400;
            margin-left: 10px;
            text-align: center;
          "
          >添加班次</el-button
        >
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        style="width: 100%; cursor: pointer"
      >
        <el-table-column prop="shift_id" label="序号" width="100">
          <template slot-scope="scope">
            {{ scope.$index + 1 + (currentPage - 1) * pageSize }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="班次名称" width="200">
        </el-table-column>
        <el-table-column prop="creator" label="创建人名称" width="200">
        </el-table-column>
        <el-table-column prop="time" label="考勤时间">
          <template scope="row">
            <span
              v-for="(item, index) in row.row.time"
              :key="index"
              style="margin-right: 5px"
              >{{ item.begin }} -{{ item.end }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <div>
              <el-button @click="editClick(scope.row)" type="text" size="small"
                >编辑</el-button
              >
              <el-button
                style="color: #ff4444"
                @click="deletePeople(scope.row)"
                type="text"
                size="small"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 4, 8]"
        :page-size="pageSize"
        layout="total, prev, pager, next, sizes"
        :total="total"
        style="float: right; margin-top: 20px"
      >
      </el-pagination>
    </div>
    <div>
      <!-- Form -->
      <el-dialog :visible.sync="editVisible" top="0">
        <el-form :model="editForm">
          <p class="titles">{{ status ? "增加班次" : "编辑班次" }}</p>
          <el-form-item label="班次名称" label-width="120px">
            <el-input v-model="editForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="创建人姓名" label-width="120px">
            <el-input v-model="editForm.creator" autocomplete="off"></el-input>
          </el-form-item>
          <p class="titles">打卡时间</p>
          <div
            style="display: flex"
            v-for="(item, index) in editForm.time"
            :key="index"
            @mouseover="showItem(index)"
            @mouseleave="hideItem(index)"
          >
            <el-form-item
              label="开始时间:"
              label-width="120px"
              style="width: 50%"
            >
              <el-time-select
                v-model="item.begin"
                :picker-options="{
                  start: '00:00',
                  step: '00:30',
                  end: '24:00',
                }"
                placeholder="选择时间"
              >
              </el-time-select>
            </el-form-item>
            <el-form-item
              label="结束时间:"
              label-width="120px"
              style="width: 50%"
            >
              <el-time-select
                v-model="item.end"
                :picker-options="{
                  start: '00:00',
                  step: '00:30',
                  end: '24:00',
                }"
                @change="Select"
                placeholder="选择时间"
              >
              </el-time-select>
            </el-form-item>
            <p
              style="
                display: inline-block;
                height: 40px;
                width: 60px;
                text-align: left;
                line-height: 38px;
              "
            >
              <i
                class="el-icon-delete"
                v-show="activeIndex === index"
                @click="deleteAttendanceTime(item, index)"
                style="
                  color: red;
                  line-height: 38px;
                  text-align: left;
                  font-size: 16px;
                "
              ></i>
            </p>
          </div>
          <p class="addTime">
            <el-button
              icon="el-icon-plus"
              @click="addTimeDuan"
              style="border-radius: 6px"
              >添加时段</el-button
            >
          </p>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button
            @click="handelEdit"
            style="
              width: 79px;
              height: 36px;
              background: #e6effa;
              border-radius: 6px;
              opacity: 1;
              font-size: 14px;
              font-family: Microsoft YaHei-Regular, Microsoft YaHei;
              font-weight: 400;
              color: #126bff;
            "
            id="reset"
            >取 消</el-button
          >
          <el-button
            id="submitForm"
            type="primary"
            @click="confirmEdit"
            style="
              width: 79px;
              height: 36px;
              background: #126bff;
              border-radius: 6px;
              opacity: 1;
              font-size: 14px;
              font-family: Microsoft YaHei-Regular, Microsoft YaHei;
              font-weight: 400;
              color: #ffffff;
            "
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <Delete
        :title="DeleteTitle"
        :dialogVisible="dialogVisible"
        @deleteConfirm="deleteConfirm"
        @handel="handel"
      ></Delete>
    </div>
  </div>
</template>
<script>
import {
  ceshi,
  addShift,
  shiftList,
  updateShift,
  delShift,
} from "@/api/CheckAttendance";
import Delete from "@/components/dialog/delete.vue";
export default {
  name: "BANCI",
  components: { Delete },
  data() {
    return {
      dialogVisible: false,
      DeleteTitle: "确认删除该班次吗？",
      formLabelWidth: "120px",
      // 按页数查询数据
      currentPage: 1,
      pageSize: 2,
      activeIndex: -1,
      total: 2,
      value1: "",
      editVisible: false,
      model: false,
      deleteItem: false,
      banciTitle: "添加班次",
      editForm: {
        shift_id: "",
        ywlsh: JSON.parse(localStorage.getItem("userInfo")).user.ywlsh,
        name: "",
        creator: "",
        time: [],
      },
      formData: {
        // 查询条件
        name: "",
        ywlsh: JSON.parse(localStorage.getItem("userInfo")).user.ywlsh,
        page: 1,
        pagesize: 2,
      },
      detaile: {},
      tableData: [], // 页面数据列表
      initTableData: [], // 数据接收
      // 时间断选择
      pickerOptions: [],
      status: true, // true 为添加  false 为编辑
      a: {
        shift_id: 2, // 班次ID
        name: "测试班次2", // 班次i名称
        creator: "张三", // 创建人姓名
        ywlsh: 2742, // 学校业务流水号
        // token:localStorage.getItem(JSON.parse(userInfo))
        time: [
          // 考勤时间
          {
            begin: "13:00",
            end: "15:00",
          },
          {
            begin: "08:00",
            end: "09:00",
          },
          {
            begin: "15:00",
            end: "16:00",
          },
        ],
        id: "",
      },
    };
  },
  watch: {
    $route(to, from) {
      console.log(to, from);
    },
  },
  created() {},
  mounted() {
    // this.ceshi();
    this.initTimeList();
  },

  methods: {
    // 删除考勤组
    deleteConfirm() {
      console.log(this.id);
      delShift({
        id: this.id,
      }).then((res) => {
        if (res.code == 200) {
          console.log(res);
          this.$message.success(res.message);
          this.initTimeList();
        } else {
          this.$message.error(res.message);
        }
      });
      this.dialogVisible = false;
      console.log("删除");
    },
    // 取消删除
    handel() {
      this.dialogVisible = false;
      console.log("取消");
    },
    // 名称查询
    submitForm(formData) {
      console.log(formData);
      this.currentPage = 1;
      this.pageSize = 2;
      this.formData.page = 1;
      this.formData.pagesize = 2;
      this.initTimeList();
    },
    // 列表重置
    resetForm() {
      this.$refs["elForm"].resetFields();
      this.currentPage = 1;
      this.pageSize = 2;
      this.formData = {
        name: "",
        ywlsh: JSON.parse(localStorage.getItem("userInfo")).user.ywlsh,
        page: 1,
        pagesize: 2,
      };
      console.log(this.formData);
      this.initTimeList();
    },
    // 数据初始化
    initTimeList() {
      this.tableData = [];
      let loadingInstance = this.$Loading.service({
        target: document.querySelector(".el-table__body-wrapper"),
      });
      shiftList(this.formData).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.res;
          this.total = res.data.count;
          console.log(this.tableData);
          loadingInstance.close();
        } else if (res.code == 400) {
          this.tableData = [];
          this.$message({
            message: res.message,
            type: "warning",
            duration: "2000",
          });
        }
      });
    },
    // 时间选择
    Select() {
      console.log(this.editForm.time);
      let isValid = true;

      for (let i = 1; i < this.editForm.time.length; i++) {
        const previousEndTime = this.editForm.time[i - 1].end;
        const currentBeginTime = this.editForm.time[i].begin;
        const currentEndTime = this.editForm.time[i].end;
        console.log(currentBeginTime <= previousEndTime);
        console.log(currentEndTime <= currentBeginTime);
        if (
          currentBeginTime <= previousEndTime ||
          currentEndTime <= currentBeginTime
        ) {
          isValid = false;
          break;
        }
      }

      if (isValid) {
        console.log("时间段设置是有效的");
      } else {
        this.$message.warning("结束时间必须大于开始时间");
        console.log("时间段设置是无效的");
      }
    },
    // 增加考勤组
    addTeam() {
      // console.log(row);
      // this.editForm = row;
      this.status = true;
      this.editForm = {
        ywlsh: JSON.parse(localStorage.getItem("userInfo")).user.ywlsh,
        name: "",
        creator: "",
        time: [],
      };
      this.editVisible = true;
    },
    // 编辑考勤组
    editClick(row) {
      this.status = false;
      this.banciTitle = "编辑班次";
      console.log(row);
      this.editForm = row;
      this.editVisible = true;
    },
    deleteAttendanceTime(item, index) {
      console.log(item);
      console.log(index);
      console.log(this.editForm.time.splice(index, 1));
    },
    // 确认编辑
    confirmEdit(e) {
      if (this.editForm.name == "") {
        this.$message.error("请输入班次名称");
        return;
      } else if (this.editForm.creator == "") {
        this.$message.error("请输入创建人姓名");
        return;
      } else if (this.editForm.time.length == 0) {
        this.$message.error("至少设置一个打卡时间");
        return;
      }

      if (this.status) {
        console.log("zengjia");
        addShift(this.editForm)
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.$message.success(res.message);
            } else {
              this.$message.warning(res.message);
            }
          })
          .then(() => {
            this.editVisible = false;
            this.initTimeList();
          });
      } else {
        console.log("111");
        updateShift(this.editForm)
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.$message.success(res.message);
            } else {
              this.$message.warning(res.message);
            }
          })
          .then(() => {
            this.editVisible = false;
            this.initTimeList();
          });
      }
    },
    // 取消编辑
    cancelEdit() {
      this.editVisible = false;
    },
    //  // 添加时间段
    addTimeDuan() {
      if (this.editForm.time.length >= 4) {
        this.$message.error("最多设置4个打卡时间");
        return;
      }
      this.editForm.time.push({
        begin: "00:00",
        end: "00:00",
      });
    },

    // 悬浮控制删除按钮的显示与隐藏
    showItem(index) {
      this.activeIndex = index;
    },
    hideItem(index) {
      this.activeIndex = -1;
    },
    handelEdit() {
      this.editVisible = false;
    },

    // 分页器相关操作
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.formData.pagesize = val;
      this.formData.page = 1;
      this.currentPage = 1;
      this.initTimeList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.formData.page = val;
      this.currentPage = val;

      this.initTimeList();
    },
    deletePeople(e) {
      this.id = e.id;
      this.dialogVisible = true;
      console.log(e);
    },
  },
};
</script>
<style lang="scss" scoped>
/deep/.el-dialog__footer {
  height: 56px !important;
  display: flex !important;
  flex-direction: row-reverse !important;
}
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: auto !important;
  min-width: 100px;
}
.warn-top {
  // border: 1px red solid;
  padding: 10px;
  background-color: white;
  border-radius: 10px;
  width: 100%;
  height: 120px;
  box-sizing: border-box;
  .el-form {
    height: 80px;
  }
}
.warn-top {
  width: calc(100%);
}
/deep/.el-form {
  height: auto;
  align-items: center;
}
.el-table {
  width: calc(100%);
}
/deep/.el-form-item__label {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
.titles {
  text-align: left;
  width: 64px;
  height: 21px;
  font-size: 16px;
  font-family: Microsoft YaHei-Bold, Microsoft YaHei;
  font-weight: bold;
  color: #13213b;
}
/deep/.el-input--medium {
  position: relative !important;
  right: 30px !important;
}
/deep/.el-range-editor--medium .el-range-separator {
  line-height: 21px;
  font-size: 14px;
}
/deep/ .el-range__icon {
  line-height: 21px;
}
/deep/.el-input--medium .el-input__icon {
  line-height: 44px !important;
}
// /deep/.el-form-item__content {
//   border: 1px red solid;
// }
.warn-content {
  margin-top: 20px;
  // border: 1px red solid;
  height: auto;
  background-color: white;
  border-radius: 10px;
  padding: 10px;
  box-sizing: border-box;
  overflow: auto;
}
/deep/.warn-content > div:nth-of-type(1) {
  display: block !important;
  padding: 10px 0 10px 0;
  text-align: left;
  // border: 1px red solid;
}
.warn-content > div:nth-of-type(1) > button:nth-of-type(1) {
  font-size: small;
  width: 108px;
  height: 36px;
  background: #126bff;
  border-radius: 8px 8px 8px 8px;
  color: #ffffff;
}
.warn-content > div:nth-of-type(1) > button:nth-of-type(2) {
  font-size: small;
  color: #ffffff;
  width: 108px;
  height: 36px;
  background: #ff4444;
  border-radius: 8px 8px 8px 8px;
}
.el-table .el-table__cell {
  padding: 12px 0;
  min-width: 0;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: center;
}
.upIpfo {
  display: flex;
  width: 100%;
  // border: 1px red solid;
  justify-content: space-between;
}
.upIpfo p {
  color: #13213b;
  font-size: 14px;
  font-weight: 400;
  width: 50%;
  margin-top: 10px;
  // border: 1px red solid;
  display: inline-block;
  // background-color: skyblue;
}
.addTime {
  text-align: center;
  height: 34px;
  .el-button {
    display: inline-block;
    width: 198px;
    height: 40px;
    background: #ffffff;
    border-radius: 6px 6px 6px 6px;
    opacity: 1;
    border: 1px solid #126bff;
    // line-height: 32px;
    font-size: 14px;
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    font-weight: 400;
    color: #126bff;
  }
}
/deep/.el-table__header-wrapper {
  // border: 1px red solid !important;
  border-radius: 10px 10px 0 0;
}
/deep/.el-table {
  border: 1px #d7dbe3 solid;
  border-radius: 10px;
}
/deep/.el-input--medium .el-input__icon {
  line-height: 30px;
}
/deep/.has-gutter tr th {
  color: black;
  text-align: center;
  border: 1px d7dbe3 solid;
  background-color: #ecf5ff;
}
/deep/.is-leaf {
  border: 1px #d7dbe3 solid !important;
  border-top: none !important;
  border-left: none !important;
}
/deep/.el-table__cell {
  text-align: center !important;
}

/deep/.el-dialog {
  border-radius: 15px !important;
  height: auto;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
/deep/.el-dialog__header {
  text-align: left;
  font-weight: 600;
}

/deep/.el-input__inner {
  border: 1px solid #d7dbe3 !important;
  background-color: white !important;
  border-radius: 6px !important;
}
::v-deep.el-table th.el-table__cell {
  background-color: #ecf5ff !important;
  width: 64px !important;
  height: 21px !important;
  font-size: 16px !important;
  color: #13213b !important;
}
</style>

